<?php
namespace app\controllers;

use app\modules\v1\controllers\BaseController;
use Yii;
use uuid;
use tools;
use yii\web\Response;
use app\modules\v1\models\ImagesGroup;
use yii\rest\ActiveController;
use app\modules\v1\models\OauthAccessToken;
/**
 * 图片分组控制器
 * @author 黄宇翔
 * @access public
 * @since 1.0
 * @return array
 */
class ImagesgroupController extends ActiveController
{

	//public $enableCsrfValidation = false;


	/**
	 * 创建分组
	 * @author 黄宇翔
	 * @access public
	 * @param mixed $name 分组名称
	 * @since 1.0
	 * @return array
	 */
	public function actionCreate()
	{
		$request = Yii::$app->request;
		$imagesGroupModel = new ImagesGroup();
		$name = $request->post('name');
		if (!$name) {
			return $this->toolsClass->prompt('400', '缺少必须的参数:name');
		}
		$unidClass = new uuid();
		$id = $unidClass->generateuuid();//生成随机id
		if ($imagesGroupModel->createGroup($id, $this->app_id, $name)) {
			return $this->toolsClass->prompt('200', 'SUCCESS', array('id' => $id));
		}
		return $this->toolsClass->prompt('500', '创建分组失败');
	}
	/**
	 * 更改分组
	 * @author 黄宇翔
	 * @access public
	 * @param mixed $name 分组名字
	 * @param mixed $group_id 分组id
	 * @since 1.0
	 * @return array
	 */
	public function actionUpdate()
	{
		$request = Yii::$app->request;
		$imagesGroupModel = new ImagesGroup();
		$name = $request->post('name');
		$groupId = $request->post('group_id');
		if (!$groupId) {
			return $this->toolsClass->prompt('400', '缺少必须的参数:group_id');
		} else if (!$name) {
			return $this->toolsClass->prompt('400', '缺少必须的参数:name');
		}
		if ($imagesGroupModel->updateGroup($name, $groupId)) {
			return $this->toolsClass->prompt('200', 'SUCCESS');
		}
		return $this->toolsClass->prompt('500', '分组更新失败');
	}

	/**
	 * 删除分组
	 * @author 黄宇翔
	 * @access public
	 * @param mixed $group_id 分组id
	 * @since 1.0
	 * @return array
	 */
	public function actionDelete()
	{
		$request = Yii::$app->request;
		$imagesGroupModel = new ImagesGroup();
		$groupId = $request->post('group_id');
		if (!$groupId) {
			return $this->toolsClass->prompt('400', '缺少必须的参数:group_id');
		}
		if ($imagesGroupModel->deleteGroup($groupId, $this->app_id)) {
			return $this->toolsClass->prompt('200', 'SUCCESS');
		}
		return $this->toolsClass->prompt('500', '分组删除失败');
	}
    /**
    * 图片获取列表
    * @author 黄宇翔
    * @access public
    * @param mixed $pageno 分页
    * @param mixed $limit 分页数
    * @since 1.0
    * @return array
    */
	public function actionIndex ()
    {
        $materialGet['pageno'] = Yii::$app->request->get('pageno');
        $materialGet['limit'] = Yii::$app->request->get('limit');
        if (!$materialGet['pageno']) {
            return $this->toolsClass->prompt('400', '请输入页数:pageno');
        } else if (!$materialGet['limit']) {
            return $this->toolsClass->prompt('400', '每页数:limit');
        }
        $imagesGroupModel = new ImagesGroup();
        $model = $imagesGroupModel->findByItems($this->app_id,$materialGet['limit'],$materialGet['pageno']);
        if ($model) {
            return $this->toolsClass->prompt('200', 'SUCCESS', $model);
        }
        return $this->toolsClass->prompt('500', '查询内部错误');

    }
}
?>